/*
 * DataEvent.java
 *
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.actions.events;

/**
 *
 * @author <a href="paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public class DataEvent {
    
    public static final int REMOVED = 0;
    public static final int INSERTED = 1;
    public static final int MOVED = 2;
    
    private int type;
    private int firstRow;
    private int lastRow;
    private int count;
    
    /** Creates a new instance of DataEvent */
    public DataEvent(int type, int firstRow, int lastRow, int count) {
        this.type = type;
        this.firstRow = firstRow;
        this.lastRow = lastRow;
        this.count = count;
    }

    public int getType() {
        return type;
    }

    public int getFirstRow() {
        return firstRow;
    }

    public int getLastRow() {
        return lastRow;
    }

    public int getCount() {
        return count;
    }

    @Override
    public String toString() {
        return "DataEvent[type=" + type + "; count=" + count + "; firstRow=" + firstRow + "; lastRow=" + lastRow + ']';
    }
    
    public int calculateCurrentRow(int oldRow) {
        
        // TODO don't know what to do if oldRow == firstRow or oldRow == lastRow
        
        // if row of event is lower in table - our position did not changed
        if (oldRow < firstRow) {
            return oldRow;
        }

        // if last row of event is higher than our - just move out row for count
        if (lastRow < oldRow) {
            if (type == INSERTED) {
                oldRow = oldRow + count;
                return oldRow;
            }
            if (type == REMOVED) {
                oldRow = oldRow - count;
                return oldRow;
            }
        }

        // we somewhere inside of shifted range
        // MOVED type is here too - there is no need to check it again
        return -1;
    }
}
